package com.baoteng.component.other;

/**
 * @file_name: ClickUtils
 * @description： 判断一定时间内是否连续点击；增大控件点击范围（使用TouchDelegate）
 */

import android.graphics.Rect;
import android.view.TouchDelegate;
import android.view.View;

public class ClickUtils {

    private static long lastClickTime;

    public static boolean isFastDoubleClick(long ms) {
        long time = System.currentTimeMillis();
        if (time - lastClickTime < ms) {
            return true;
        }
        lastClickTime = time;
        return false;
    }


    public static void expandViewTouchDelegate(View view, int range) {

        if (range <= 0) {
            return;
        }

        expandViewTouchDelegate(view, range, range, range, range);
    }

    /**
     * 扩大View的触摸和点击响应范围,最大不超过其父View范围
     * 1.若View的自定义触摸范围超出Parent的大小，则超出的那部分无效。
     * 2.一个Parent只能设置一个View的TouchDelegate，设置多个时只有最后设置的生效。
     *
     * @param view
     * @param top
     * @param bottom
     * @param left
     * @param right
     */
    public static void expandViewTouchDelegate(final View view, final int top,
                                               final int bottom, final int left, final int right) {

        ((View) view.getParent()).post(new Runnable() {
            @Override
            public void run() {
                Rect bounds = new Rect();
                view.setEnabled(true);
                view.getHitRect(bounds);

                bounds.top -= top;
                bounds.bottom += bottom;
                bounds.left -= left;
                bounds.right += right;

                TouchDelegate touchDelegate = new TouchDelegate(bounds, view);

                if (View.class.isInstance(view.getParent())) {
                    ((View) view.getParent()).setTouchDelegate(touchDelegate);
                }
            }
        });
    }
}
